//
//  MainViewController.m
//  AILock
//
//  Created by 谭亚明on 2020/6/17.
//  Copyright © 2020 tym. All rights reserved.
//

#import "MainViewController.h"
#import "UserTool.h"
#import "YYModel.h"
#import "FamilyManageVC.h"
#import "OpenLockVC.h"
#import "MessageHistoryVC.h"
#import "NSString+Option.h"
#import "UserModel.h"
#import "TYMNavigationController.h"
#import "HomeViewController.h"
#import "MineViewController.h"
#import "SceneViewController.h"
#import "Fonts.h"
#import "LoginViewController.h"
#import "UIColor+HXExtension.h"
@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    HomeViewController *homeview = [[HomeViewController alloc] init];
    TYMNavigationController *homeNavi = [[TYMNavigationController alloc] initWithRootViewController:homeview];
    
    MineViewController *mineView = [[MineViewController alloc] init];
    TYMNavigationController *mineNavi = [[TYMNavigationController alloc] initWithRootViewController:mineView];
    
    self.viewControllers = @[homeNavi,mineNavi];
    
    NSArray* tabBarTitles = @[@"首页",@"我的"];
    [self.viewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull vc, NSUInteger idx, BOOL * _Nonnull stop) {
        vc.tabBarItem.tag = idx;
        
        vc.tabBarItem.title = [tabBarTitles objectAtIndex:idx];
        vc.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -5);
        [vc.tabBarItem setTitleTextAttributes:@{NSFontAttributeName :Font_Regular(9),NSForegroundColorAttributeName:[UIColor hx_colorWithHexStr:@"#868686"] } forState:UIControlStateNormal];
        
        [vc.tabBarItem setTitleTextAttributes:@{NSFontAttributeName :Font_Regular(9),NSForegroundColorAttributeName:[UIColor hx_colorWithHexStr:@"#4C84FF"] } forState:UIControlStateSelected];
        
        if (idx == 0) {
            [vc.tabBarItem setImage:[[UIImage imageNamed:@"icon_tabbar_home_nor"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
            [vc.tabBarItem setSelectedImage:[[UIImage imageNamed:@"icon_tabbar_home_sel"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        }else if(idx == 1){
            [vc.tabBarItem setImage:[[UIImage imageNamed:@"icon_tabbar_mine_nor"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
            [vc.tabBarItem setSelectedImage:[[UIImage imageNamed:@"icon_tabbar_mine_sel"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        }
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginView:) name:@"showLoginView" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didJPumpToDetail:) name:@"didJPumpToDetail" object:nil];
}

-(void)loginView:(NSNotification *) notification{
    
    UINavigationController *naviController = self.childViewControllers[self.selectedIndex];
    if (naviController.childViewControllers.count) {
        [naviController popToRootViewControllerAnimated:NO];
    }
    if (![UserTool getLogInUser] ||
        ![UserTool getCurrentFamily]) {
        LoginViewController *loginVC = [LoginViewController new];
        if (notification.object) {
            loginVC.isWellcom = YES;
        }
        TYMNavigationController  *navi = [[TYMNavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:navi animated:YES completion:nil];
    }
}

-(void)didJPumpToDetail:(NSNotification *) notification{
    
    NSDictionary *dataDic = [NSString dictionaryWithJsonString:notification.object[@"Json"]];
    EquipmentModel *equip = [EquipmentModel yy_modelWithJSON:dataDic[@"data"]];
    switch ([dataDic[@"code"] intValue]) {
        case 28: //开关锁上报
        {
            MessageHistoryVC *historyVC = [[MessageHistoryVC alloc] initWithModel:equip];
            TYMNavigationController  *navi = [[TYMNavigationController alloc] initWithRootViewController:historyVC];
            [self presentViewController:navi animated:YES completion:nil];
        }
            break;
        case 29: //事件报警
        {
            MessageHistoryVC *historyVC = [[MessageHistoryVC alloc] initWithModel:equip index:1];
            TYMNavigationController  *navi = [[TYMNavigationController alloc] initWithRootViewController:historyVC];
            [self presentViewController:navi animated:YES completion:nil];
        }
            
            break;
        case 50: //本地开关门申请
        {
            OpenLockVC *openVC = [[OpenLockVC alloc] initWithEquipment:equip];
            TYMNavigationController  *navi = [[TYMNavigationController alloc] initWithRootViewController:openVC];
            [self presentViewController:navi animated:YES completion:nil];
        }
            break;
        case 38: //本地初始化
        {
            MessageHistoryVC *historyVC = [[MessageHistoryVC alloc] initWithModel:equip index:2];
            TYMNavigationController  *navi = [[TYMNavigationController alloc] initWithRootViewController:historyVC];
            [self presentViewController:navi animated:YES completion:nil];
        }
            break;
        case 112:
        {
            FamilyManageVC *manageVC = [[FamilyManageVC alloc] init];
            TYMNavigationController  *navi = [[TYMNavigationController alloc] initWithRootViewController:manageVC];
            [self presentViewController:navi animated:YES completion:nil];
        }
            break;
    }
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
